<?php
/**
 * Created by PhpStorm.
 * User: nightelf
 * Date: 2020/1/28
 * Time: 21:03
 */

namespace App\Http\Model;


use Illuminate\Support\Facades\DB;

class WarehouseLogInfoModel
{
    const TABLE = 'warehouse_log_info';

    /**
     * @param array $data
     * @return mixed
     */
    public function lists(array $data)
    {
        $result = DB::table(self::TABLE)
            ->leftJoin('warehouse_log as wl', 'wl.id', '=', self::TABLE . '.logId')
            ->leftJoin('material as m', 'm.id', '=', self::TABLE . '.materialId')
            ->leftJoin('material_spec as spec', 'spec.id', '=', self::TABLE . '.specId')
            ->leftJoin('supplier as s', 's.id', '=','.wl.sourceSupplierId')
            ->leftJoin('project as p', 'p.id', '=', 'wl.projectId')
            ->leftJoin('project as p1', 'p1.id', '=', 'wl.sourceProjectId')
            ->leftJoin('group as g', 'g.id', '=', 'wl.sourceGroupId')
            ->leftJoin('employee as e', 'e.id', '=', 'wl.sourceEmployeeId')
            ->leftJoin('unit as u', 'u.id', '=', self::TABLE . '.unitId')
            ->where(function ($query) use ($data) {
                if (isset($data['projectId']) && !empty($data['projectId'])) {
                    $query->where('wl.projectId', $data['projectId']);
                }
                if (isset($data['sourceProjectId']) && !empty($data['sourceProjectId'])) {
                    $query->where('wl.sourceProjectId', $data['sourceProjectId']);
                }
                if (isset($data['sourceEmployeeId']) && !empty($data['sourceEmployeeId'])) {
                    $query->where('wl.sourceEmployeeId', $data['sourceEmployeeId']);
                }
                if (isset($data['sourceGroupId']) && !empty($data['sourceGroupId'])) {
                    $query->where('wl.sourceGroupId', $data['sourceGroupId']);
                }
                if (isset($data['sourceSupplierId']) && !empty($data['sourceSupplierId'])) {
                    $query->where('wl.sourceSupplierId', $data['sourceSupplierId']);
                }
                if (!empty($data['type'])) {
                    if (is_array($data['type'])){
                        $query->whereIn('wl.type', $data['type']);
                    }else{
                        $query->where('wl.type', $data['type']);
                    }
                }
                if (!empty($data['materialId'])){
                    $query->where(self::TABLE.'.materialId', $data['materialId']);
                }else{
                    if (!empty($data['materialType'])) {
                        $query->where('m.categoryId', $data['materialType']);
                    }
                    if (isset($data['search']) && !is_null($data['search'])) {
                        $query->where(function($query1) use ($data){
                            $query1->where('m.name', 'like', '%' . $data['search'] . '%')
                            ->orWhere('spec.spec', 'like', '%' . $data['search'] . '%');
                        });

                    }
                }
                if (isset($data['specId']) && !empty($data['specId'])) {
                    $query->where(self::TABLE.'.specId', $data['specId']);
                }
                if (isset($data['status']) && !is_null($data['status'])) {
                    $query->where('wl.status', $data['status']);
                }
                if (!empty($data['month'])) {
                    $startTime = date($data['month'] . '-01 00:00:00');
                    $endTime = date($data['month'] . '-t 23:59:59');
                    $query->whereBetween('wl.time', [$startTime, $endTime]);
                } else {
                    if (isset($data['startTime']) && !is_null($data['startTime'])) {
                        $query->where('wl.time', '>=', $data['startTime'] . " 00:00:00");
                    }
                    if (isset($data['endTime']) && !is_null($data['endTime'])) {
                        $query->where('wl.time', '<=', $data['endTime'] . " 23:59:59");
                    }
                }
            });
        if (!empty($data['length']) && !empty($data['page'])) {
            $limit = config('yucheng.limit');
            $start = 0;
            if (!empty($data['length'])) {
                $limit = $data['length'];
            }
            if (!empty($data['page'])) {
                $start = ($data['page'] - 1) * $limit;
            }
            $result = $result->offset($start)->limit($limit);
        }
        return $result->orderBy('wl.time', 'desc')->orderBy(self::TABLE.'.id', 'desc')
            ->select(self::TABLE . '.*',
                'u.name as unitName',
                'm.name as materialName', 'spec.spec', 's.name as sourceSupplierName',
                'p.name as projectName', 'p1.name as sourceProjectName', 'e.name as sourceEmployeeName', 'g.name as sourceGroupName',
                'wl.projectId', 'wl.type', 'wl.sourceEmployeeId', 'wl.sourceProjectId', 'wl.time', 'wl.createTime', 'wl.status', 'wl.remark', 'wl.recoveryFunds')
            ->get()->toArray();
    }

    /**
     * @param array $data
     * @return mixed
     */
    public function countLists(array $data)
    {
        return DB::table(self::TABLE)
            ->leftJoin('warehouse_log as wl', 'wl.id', '=', self::TABLE . '.logId')
            ->leftJoin('material as m', 'm.id', '=', self::TABLE . '.materialId')
            ->leftJoin('material_spec as spec', 'spec.id', '=', self::TABLE . '.specId')
            ->where(function ($query) use ($data) {
                if (isset($data['projectId']) && !empty($data['projectId'])) {
                    $query->where('wl.projectId', $data['projectId']);
                }
                if (isset($data['sourceProjectId']) && !empty($data['sourceProjectId'])) {
                    $query->where('wl.sourceProjectId', $data['sourceProjectId']);
                }
                if (isset($data['sourceEmployeeId']) && !empty($data['sourceEmployeeId'])) {
                    $query->where('wl.sourceEmployeeId', $data['sourceEmployeeId']);
                }
                if (isset($data['sourceGroupId']) && !empty($data['sourceGroupId'])) {
                    $query->where('wl.sourceGroupId', $data['sourceGroupId']);
                }
                if (!empty($data['type'])) {
                    if (is_array($data['type'])){
                        $query->whereIn('wl.type', $data['type']);
                    }else{
                        $query->where('wl.type', $data['type']);
                    }
                }
                if (!empty($data['materialId'])){
                    $query->where(self::TABLE.'.materialId', $data['materialId']);
                }else{
                    if (!empty($data['materialType'])) {
                        $query->where('m.categoryId', $data['materialType']);
                    }
                    if (isset($data['search']) && !is_null($data['search'])) {
                        $query->where(function($query1) use ($data){
                            $query1->where('m.name', 'like', '%' . $data['search'] . '%')
                                ->orWhere('spec.spec', 'like', '%' . $data['search'] . '%');
                        });

                    }
                }
                if (isset($data['status']) && !is_null($data['status'])) {
                    $query->where('wl.status', $data['status']);
                }
                if (!empty($data['month'])) {
                    $startTime = date($data['month'] . '-01 00:00:00');
                    $endTime = date($data['month'] . '-t 23:59:59');
                    $query->whereBetween('wl.time', [$startTime, $endTime]);
                } else {
                    if (isset($data['startTime']) && !is_null($data['startTime'])) {
                        $query->where('wl.time', '>=', $data['startTime'] . " 00:00:00");
                    }
                    if (isset($data['endTime']) && !is_null($data['endTime'])) {
                        $query->where('wl.time', '<=', $data['endTime'] . " 23:59:59");
                    }
                }
            })
            ->count();
    }

    /**
     * @param array $data
     * @return mixed
     */
    public function insert(array $data)
    {
        return DB::table(self::TABLE)->insert($data);
    }

    /**
     * @param array $data
     * @return mixed
     */
    public function info(array $data)
    {
        $result = DB::table(self::TABLE)->where($data)->first();
        return empty($result)?[]:get_object_vars($result);
    }
}